/*
 * Copyright 2022 The Open Islands Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.openislands.oi.util;

import org.openislands.oi.pojo.dto.OfNodeTaskDTO;

import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;

public class DirectoryUtils {
    private static Path projectRootPath = null;

    static {
        getProjectRootPath();
    }

    public static Path getProjectRootPath() {
        if (projectRootPath != null){
            return projectRootPath;
        }else{
            projectRootPath = FileSystems.getDefault().getPath("").toAbsolutePath();
        }
        return projectRootPath;
    }

    public static Path genRunTempPath(String... args){
        return genSpecifyPath("run", args);
    }

    public static Path genLogPath(String... args){
        return genSpecifyPath("logs", args);
    }

    public static Path genTaskConfigPath(OfNodeTaskDTO ofNodeTaskDTO){
        return genRunTempPath("job", ofNodeTaskDTO.getJobId(), "config", ofNodeTaskDTO.getTaskName(), ofNodeTaskDTO.getTaskVersion().toString(), ofNodeTaskDTO.getRole().name().toLowerCase(), ofNodeTaskDTO.getNodeId());
    }

    public static String[] genTaskLogPathItem(OfNodeTaskDTO ofNodeTaskDTO) {
        return Arrays.asList(ofNodeTaskDTO.getJobId(), ofNodeTaskDTO.getTaskName(), ofNodeTaskDTO.getTaskVersion().toString(), ofNodeTaskDTO.getRole().name().toLowerCase(), ofNodeTaskDTO.getNodeId()).toArray(new String[0]);
    }

    public static Path genTaskLogPath(OfNodeTaskDTO ofNodeTaskDTO){
        return genLogPath("job", ofNodeTaskDTO.getJobId(), ofNodeTaskDTO.getTaskName(), ofNodeTaskDTO.getTaskVersion().toString(), ofNodeTaskDTO.getRole().name().toLowerCase(), ofNodeTaskDTO.getNodeId());
    }

    public static Path genTaskOutputDataDirPath(OfNodeTaskDTO ofNodeTaskDTO){
        return Paths.get("data", "output", ofNodeTaskDTO.getJobId(), ofNodeTaskDTO.getTaskName(), ofNodeTaskDTO.getTaskVersion().toString(), ofNodeTaskDTO.getRole().name().toLowerCase(), ofNodeTaskDTO.getNodeId());
    }

    public static Path genTaskOutputModelDirPath(OfNodeTaskDTO ofNodeTaskDTO){
        return Paths.get( "model", "output", ofNodeTaskDTO.getJobId(), ofNodeTaskDTO.getTaskName(), ofNodeTaskDTO.getTaskVersion().toString(), ofNodeTaskDTO.getRole().name().toLowerCase(), ofNodeTaskDTO.getNodeId());
    }

    public static Path getLocalOperatorDirectory(){
        return Paths.get(projectRootPath.toString(), "operators");
    }

    public static Path getDefaultPyoiPath(){
        return Paths.get(projectRootPath.toString(), "core", "python");
    }

    public static String getOperatorDefaultPythonpath(String sourceUri){
        return String.format("%s:%s", UriUtils.trimFileUri(sourceUri), getDefaultPyoiPath().toString());
    }

    private static Path genSpecifyPath(String typeName, String... args){
        return Paths.get(Paths.get(projectRootPath.toString(), typeName).toString(), args);
    }
}
